﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Ionic.Zip;

namespace TestZip
{
    public partial class Zipfile : Form
    {
        public Zipfile()
        {
            InitializeComponent();
        }

        private void BT_Add_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFile = new OpenFileDialog();
            openFile.Filter = "All file|*.*";
            openFile.Multiselect = true;
            if (openFile.ShowDialog() == DialogResult.OK)
            {
                foreach (var sFile in openFile.FileNames)
                {
                    checkedListBox1.Items.Add(sFile, CheckState.Checked);
                }
            }
        }

        private void BT_Remove_Click(object sender, EventArgs e)
        {
            if (checkedListBox1.Items.Count > 0)
            {
                checkedListBox1.Items.RemoveAt(checkedListBox1.SelectedIndex);
            }
        }

        private void BT_Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFile = new SaveFileDialog();
            saveFile.Filter = "zip file|*.zip";
            try
            {
                if (saveFile.ShowDialog() == DialogResult.OK)
                {
                    using (ZipFile zip = new ZipFile())
                    {
                        foreach (var item in checkedListBox1.CheckedItems)
                        {
                            zip.AddFile(item.ToString(),"\\");
                        }
                        zip.Save(saveFile.FileName);
                        MessageBox.Show("save ok!", "zip file", MessageBoxButtons.OK,MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("save error: " + ex.Message, "zip file", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                throw;
            }
            finally
            {
                saveFile.Dispose();
            }

        }

        private void BT_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void BT_Unzip_Click(object sender, EventArgs e)
        {
            UnZip uForm = new UnZip();
            uForm.ShowDialog();
        }
    }
}
